package com.projectzqjz.parttimework.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.projectzqjz.parttimework.R;
import com.projectzqjz.parttimework.entity.PartAddressEntity;

import java.util.ArrayList;
import java.util.List;

public class FenleiLeftAdapter extends RecyclerView.Adapter<FenleiLeftAdapter.ViewHolder> {
    //1、定义一个集合，用来记录选中
    private List<Boolean> isClicks;
    private Context context;
    private List<PartAddressEntity> list;
    private String type = "";
    //2、定义监听并设set方法
    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public FenleiLeftAdapter(Context context, List<PartAddressEntity> list,String type) {
        this.context = context;
        this.list = list;
        this.type=type;
        //3、为集合添加值
        isClicks = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            isClicks.add(false);
        }
    }

    //创建新View，被LayoutManager所调用
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_part_address, viewGroup, false);

        return new ViewHolder(view);
    }

    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, int position) {
        viewHolder.tvName.setText(list.get(position).getName());

        //4：设置点击事件
        if (mOnItemClickListener != null) {
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = viewHolder.getLayoutPosition(); // 1
                    for (int i = 0; i < isClicks.size(); i++) {
                        isClicks.set(i, false);
                    }
                    isClicks.set(position, true);
                    notifyDataSetChanged();
                    mOnItemClickListener.onItemClick(viewHolder.itemView, position); // 2
                }
            });
        }
        //5、记录要更改属性的控件
        //6、判断改变属性
        if (isClicks.get(position)) {
            if ("2".equals(type)){
                viewHolder.line_bg.setVisibility(View.GONE);
            }else {
                viewHolder.line_bg.setVisibility(View.VISIBLE);
            }
            viewHolder.tvName.setTextColor(Color.parseColor("#FB5444"));
        } else {
            viewHolder.line_bg.setVisibility(View.GONE);
            viewHolder.tvName.setTextColor(Color.parseColor("#5A5A5A"));
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvName;
        TextView line_bg;

        ViewHolder(View view) {
            super(view);
            tvName = (TextView) view.findViewById(R.id.tv_name);
            line_bg = (TextView) view.findViewById(R.id.line_bg);
        }
    }

    //7、定义点击事件回调接口
    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

}
